//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "FdmInnerValueCalculator.h"
using namespace Cephei::QL::Methods::Finitedifferences::Utilities;
#include <gen/QL/Methods/Finitedifferences/Operators/FdmLinearOpIterator.h>
using namespace Cephei::QL::Methods::Finitedifferences::Operators;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::CFdmInnerValueCalculator (boost::shared_ptr<QuantLib::FdmInnerValueCalculator>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmInnerValueCalculator = NULL;
#endif
	_ppFdmInnerValueCalculator = &childNative;
    
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::CFdmInnerValueCalculator (QuantLib::FdmInnerValueCalculator& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmInnerValueCalculator = NULL;
#endif
	_ppFdmInnerValueCalculator = new boost::shared_ptr<QuantLib::FdmInnerValueCalculator> (&childNative);
    
    _FdmInnerValueCalculatorOwner = owner;
    
}

Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::CFdmInnerValueCalculator (CFdmInnerValueCalculator^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmInnerValueCalculator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmInnerValueCalculator = new boost::shared_ptr<QuantLib::FdmInnerValueCalculator> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::CFdmInnerValueCalculator (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmInnerValueCalculator = NULL;
#endif
	if (!t->IsSubclassOf(CFdmInnerValueCalculator::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::CFdmInnerValueCalculator (QuantLib::Handle<QuantLib::FdmInnerValueCalculator>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmInnerValueCalculator = &childNative;
	_ppFdmInnerValueCalculator = &static_cast<boost::shared_ptr<QuantLib::FdmInnerValueCalculator>>(childNative.currentLink());
    
    _FdmInnerValueCalculatorOwner = owner;
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::CFdmInnerValueCalculator (QuantLib::Handle<QuantLib::FdmInnerValueCalculator> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmInnerValueCalculator = &childNative;
	_ppFdmInnerValueCalculator = &static_cast<boost::shared_ptr<QuantLib::FdmInnerValueCalculator>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::CFdmInnerValueCalculator (QuantLib::FdmInnerValueCalculator childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmInnerValueCalculator = NULL;
#endif
	_ppFdmInnerValueCalculator = new boost::shared_ptr<QuantLib::FdmInnerValueCalculator> (new QuantLib::FdmInnerValueCalculator (childNative));
    
}
#endif

Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::~CFdmInnerValueCalculator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmInnerValueCalculator != NULL)
    {
	    delete _ppFdmInnerValueCalculator;
        _ppFdmInnerValueCalculator = NULL;
    }
}
Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::!CFdmInnerValueCalculator ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmInnerValueCalculator != NULL)
    {
	    delete _ppFdmInnerValueCalculator;
    }
}
QuantLib::FdmInnerValueCalculator& Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::GetReference ()
{
    if (_ppFdmInnerValueCalculator == NULL) throw REFNEW NativeNullException ();
	return **_ppFdmInnerValueCalculator;
}
boost::shared_ptr<QuantLib::FdmInnerValueCalculator>& Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::GetShared ()
{
    if (_ppFdmInnerValueCalculator == NULL) throw REFNEW NativeNullException ();
	return *_ppFdmInnerValueCalculator;
}
QuantLib::FdmInnerValueCalculator* Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::GetPointer ()
{
    if (_ppFdmInnerValueCalculator == NULL) throw REFNEW NativeNullException ();
	return &**_ppFdmInnerValueCalculator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmInnerValueCalculator>& Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::GetHandle ()
{
	if (_phFdmInnerValueCalculator == NULL)
	{
		_phFdmInnerValueCalculator = new Handle<QuantLib::FdmInnerValueCalculator> (*_ppFdmInnerValueCalculator);
	}
	return *_phFdmInnerValueCalculator;
}
#endif
bool Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::HasNative () 
{
	return (_ppFdmInnerValueCalculator != NULL);
}

Double Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::AvgInnerValue (Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpIterator^ iter, Double t)
{
    CFdmLinearOpIterator^ _Citer;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Citer = safe_cast<CFdmLinearOpIterator^> (iter);
        _Citer->Lock();
        QuantLib::FdmLinearOpIterator& _iter = static_cast<QuantLib::FdmLinearOpIterator&> (_Citer->GetReference ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmInnerValueCalculator)->avgInnerValue ( _iter,  _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Citer != nullptr) _Citer->Unlock();
    }
}
Double Cephei::QL::Methods::Finitedifferences::Utilities::CFdmInnerValueCalculator::InnerValue (Cephei::QL::Methods::Finitedifferences::Operators::IFdmLinearOpIterator^ iter, Double t)
{
    CFdmLinearOpIterator^ _Citer;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Citer = safe_cast<CFdmLinearOpIterator^> (iter);
        _Citer->Lock();
        QuantLib::FdmLinearOpIterator& _iter = static_cast<QuantLib::FdmLinearOpIterator&> (_Citer->GetReference ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppFdmInnerValueCalculator)->innerValue ( _iter,  _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Citer != nullptr) _Citer->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

